package goescape

import "fmt"

type (
	cursor struct {
		// Move cursor.
		MoveTo moveTo
		// Restore saved position.
		Restore,
		// Save position.
		Save,
		// Show hidden cursor.
		Show,
		// Hide cursor.
		Hide,
		// Get cursor position.
		Get Escape
	}

	moveTo struct {
		// Up n lines.
		Up,
		// Down n lines.
		Down,
		// Right by n positions.
		Right,
		// Left by n positions.
		Left,
		// Next line n times.
		NextLine,
		// Previous line n times.
		PreviousLine,
		// Column x.
		Horizontal func(x uint16) Escape
		// Column x and line y.
		Position func(x, y uint16) Escape
	}
)

// Return escape code for cursor control.
func Cursor() cursor {
	return cursor{
		MoveTo: moveTo{
			Up: func(n uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dA", n))
			},
			Down: func(n uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dB", n))
			},
			Right: func(n uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dC", n))
			},
			Left: func(n uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dD", n))
			},
			NextLine: func(n uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dE", n))
			},
			PreviousLine: func(n uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dF", n))
			},
			Horizontal: func(x uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%dG", x))
			},
			Position: func(x, y uint16) Escape {
				return Escape(fmt.Sprintf("\x1b[%d;%dH", y, x))
			},
		},
		Restore: "\x1b[u",
		Save:    "\x1b[s",
		Show:    "\x1b[?25h",
		Hide:    "\x1b[?25l",
		Get:     "\x1b[6n",
	}
}
